package com.spx.core.handlers;

import java.util.Collection;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;

import com.spx.core.CoreActivator;
import com.spx.core.types.Activity;
import com.spx.core.types.Role;

public class DeleteActionHandler extends DeleteModelTypeHandler<Activity>
{

	@Override
	protected void delete(Activity model)
	{
		boolean exists = false;

		final Collection<Role> allRoles = CoreActivator.getDefault().getSecurityService().fetchAllRoles(0, Integer.MAX_VALUE);

		roleLoop: for (final Role role : allRoles)
		{
			for (final Activity activity : role.getActions())
			{
				if (activity.equals(model))
				{
					exists = true;
					break roleLoop;
				}
			}
		}

		if (exists == true)
		{

			final Status status = new Status(IStatus.ERROR, CoreActivator.PLUGIN_ID, 0, "Activity is Use", null);
			ErrorDialog.openError(shell, "Cannot Delete", "Cannot Delete Activity", status);

		} else
		{
			CoreActivator.getDefault().getSecurityService().delete(model);
		}
	}

}
